package com.example.carrierapp.util;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.lb.fingerdemo.R;

public class ToastUtils {

    private static Toast shortToast = null;
    private static Toast mToast;
    private static TextView mTvMsg;

    public static void makeShortText(String msg, Context context) {
        if (context == null) {
            return;
        }

        if (shortToast == null) {
            shortToast = Toast.makeText(context.getApplicationContext(), msg, Toast.LENGTH_SHORT);
        } else {
            shortToast.setText(msg);
        }
        shortToast.show();
    }


    private static Toast longToast = null;

    public static void makeLongText(String msg, Context context) {
        if (context == null) {
            return;
        }

        if (longToast == null) {
            longToast = Toast.makeText(context.getApplicationContext(), msg, Toast.LENGTH_LONG);
        } else {
            longToast.setText(msg);
        }
        longToast.show();
    }


    public static void showLong(Context context, String msg) {
        makeLongText(msg, context);
    }

    public static void showLong(Context context, int id) {
        makeLongText(context.getResources().getString(id), context);
    }

    public static void showShort(Context context, String msg) {
        makeShortText(msg, context);
    }

    public static void showShort(Context context, int id) {
        makeShortText(context.getResources().getString(id), context);
    }


    /**
     * 自定义带图片和文字的Toast，最终的效果就是上面是图片，下面是文字
     *
     * @param context
     * @param message
     */
    public static void showCenterToast( String message,Context context) {
        if (mToast == null) {
            mToast = new Toast(context);
            RelativeLayout rl = (RelativeLayout) LayoutInflater.from(context).inflate(R.layout.layout_toast_center, null);
            mTvMsg = rl.findViewById(R.id.tv_toast);
            mToast.setView(rl);
        }
        if (mTvMsg != null)
            mTvMsg.setText(message);
        if (mToast != null) {
            mToast.setGravity(Gravity.CENTER, 0, 0);
            mToast.setDuration(Toast.LENGTH_SHORT);
            mToast.show();
        }
    }

    public static void showCenterToast( Context context,String message) {
        showCenterToast(message,context);
    }

}
